package com.zoespeer.food_delivery_comment_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zoespeer.food_delivery_comment_api.entity.Review;
import com.zoespeer.food_delivery_comment_api.mapper.ReviewMapper;
import com.zoespeer.food_delivery_comment_api.service.ReviewService;
import com.zoespeer.food_delivery_comment_api.vo.MyReviewVO;
import com.zoespeer.food_delivery_comment_api.vo.PublishCommandRequestVO;
import com.zoespeer.food_delivery_comment_api.vo.UserIdResponseVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReviewServiceImpl implements ReviewService {

    private static final Logger logger = LoggerFactory.getLogger(ReviewServiceImpl.class);

    @Autowired
    private ReviewMapper reviewMapper;

    @Override
    public boolean publishCommand(PublishCommandRequestVO request, Integer userId) {
        Review review = new Review();
        review.setUserId(userId);
        review.setRestaurantId(request.getCanteenId());
        review.setRating(request.getScore());
        review.setContent(request.getContent());
        review.setLikeCount(0);
        review.setStatus("pending");
        review.setCreatedAt(LocalDateTime.now());
        review.setUpdatedAt(LocalDateTime.now());
        return reviewMapper.insert(review) > 0;
    }

    @Override
    public List<MyReviewVO> getMyReviews(String userId) {
        if (userId == null || userId.trim().isEmpty()) {
            throw new RuntimeException("用户ID不能为空");
        }
        logger.info("开始查询用户[{}]的评论", userId);
        List<MyReviewVO> result = reviewMapper.selectMyReviews(userId);
        logger.info("查询到用户[{}]的评论共{}条", userId, result.size());
        return result;
    }

    @Override
    public void deleteReview(Integer reviewId, Integer userId) {
        Review review = reviewMapper.selectById(reviewId);
        if (review == null) {
            throw new RuntimeException("评论不存在或已被删除");
        }

        if (!review.getUserId().equals(userId)) {
            throw new RuntimeException("无权限删除该评论");
        }

        int rows = reviewMapper.deleteById(reviewId);
        if (rows <= 0) {
            throw new RuntimeException("删除失败，请重试");
        }
    }
    @Override
    public UserIdResponseVO getUserIdByReviewId(Integer reviewId) {
        if (reviewId == null || reviewId <= 0) {
            throw new RuntimeException("评论ID必须为正整数");
        }

        // 从数据库查询到的是Integer类型userId
        Integer userIdInt = reviewMapper.getUserIdByReviewId(reviewId);
        if (userIdInt == null) {
            throw new RuntimeException("未找到该评论对应的用户ID");
        }

        // 转换为String类型
        String userIdStr = userIdInt.toString();

        // 封装到VO（String类型）
        UserIdResponseVO responseVO = new UserIdResponseVO();
        responseVO.setUserId(userIdStr);
        return responseVO;
    }

}